package org.tao.lightningmq.nameserver.core;

import org.tao.lightningmq.common.constants.BrokerConstants;
import org.tao.lightningmq.nameserver.common.CommonCache;
import org.tao.lightningmq.nameserver.common.MasterSlaveReplicationProperties;
import org.tao.lightningmq.nameserver.common.NameserverProperties;
import org.tao.lightningmq.nameserver.common.TraceReplicationProperties;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Properties;

/**
 * @Author lht
 * @date 2025/1/8 - 15:20
 * @description:
 */
public class PropertiesLoader {

    private final Properties properties = new Properties();

    public void loadProperties() throws IOException {
        String mqHome = System.getenv(BrokerConstants.MQ_HOME);
        properties.load(Files.newInputStream(new File(mqHome + "/config/nameserver.properties").toPath()));
        NameserverProperties nameserverProperties = new NameserverProperties();
        nameserverProperties.setNameserverPwd(getStr("nameserver.password"));
        nameserverProperties.setNameserverUser(getStr("nameserver.user"));
        nameserverProperties.setNameserverPort(getInt("nameserver.port"));
        nameserverProperties.setReplicationMode(getStrCanBeNull("nameserver.replication.mode"));
        TraceReplicationProperties traceReplicationProperties = new TraceReplicationProperties();
        traceReplicationProperties.setNextNode(getStrCanBeNull("nameserver.replication.trace.next.node"));
        traceReplicationProperties.setPort(getIntCanBeNull("nameserver.replication.trace.port"));
        nameserverProperties.setTraceReplicationProperties(traceReplicationProperties);
        MasterSlaveReplicationProperties masterSlaveReplicationProperties = new MasterSlaveReplicationProperties();
        masterSlaveReplicationProperties.setMaster(getStrCanBeNull("nameserver.replication.master"));
        masterSlaveReplicationProperties.setRole(getStrCanBeNull("nameserver.replication.master.slave.role"));
        masterSlaveReplicationProperties.setType(getStrCanBeNull("nameserver.replication.master.slave.type"));
        masterSlaveReplicationProperties.setPort(getInt("nameserver.replication.port"));
        nameserverProperties.setMasterSlaveReplicationProperties(masterSlaveReplicationProperties);
        CommonCache.setNameserverProperties(nameserverProperties);
        nameserverProperties.print();
    }

    private String getStr(String key) {
        String value = properties.getProperty(key);
        if (value == null) {
            throw new RuntimeException("配置参数：" + key + "不存在");
        }
        return value;
    }

    private Integer getIntCanBeNull(String key) {
        String intValue = getStr(key);
        return Integer.valueOf(intValue);
    }

    private Integer getInt(String key) {
        return Integer.valueOf(getStr(key));
    }

    public String getProperty(String key) {
        return properties.getProperty(key);
    }

    private String getStrCanBeNull(String key) {
        String value = properties.getProperty(key);
        if (value == null) {
            return "";
        }
        return value;
    }

}
